package com.ai.rag.qa.springaialibabaragqa.controller;

import com.ai.rag.qa.springaialibabaragqa.assistant.AiAssistant;
import jakarta.annotation.Resource;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.redis.RedisVectorStore;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserChatController {

    @Resource
    private AiAssistant aiAssistant;

    @Resource
    private RedisVectorStore redisVectorStore;

    @GetMapping("/chatQuery")
    public String chatQuery(@RequestParam String query) {
//        构建请求，获取相似度前三的文档
        SearchRequest.Builder searchRequest = SearchRequest.builder().query(query).topK(3);
        List<Document> documents = redisVectorStore.similaritySearch(searchRequest.build());
        return aiAssistant.chat(query, documents);
    }

}

